import time
from copy import deepcopy

import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning

from cookie_pool import CookiePool
from robot import DingtalkRobot

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)


class AutoMsg(object):
    robot = DingtalkRobot()
    cookie_pool = CookiePool()

    def __init__(self):
        # self.proxies = {"http": "http://127.0.0.1:8888", "https": "http://127.0.0.1:8888"}
        self.proxies = None
        self.headers = {
            "accept": "application/json",
            "accept-language": "zh-CN",
            "accept-encoding": "gzip",
            "Host": "api.stepn.io"
        }

    def get_newest_sneakers_order_list(self):
        """
        获取最新数据
        """
        headers = deepcopy(self.headers)
        cookie = self.cookie_pool.get_cookie_str()
        print(f'cookie：{cookie}')
        headers.update({'cookie': cookie})
        newest_order_list_url = 'https://api.stepn.io/run/orderlist?order=2001&refresh=true'
        res = requests.get(newest_order_list_url, headers=headers, verify=False, proxies=self.proxies)

        data = res.json().get("data") or []
        if not data:
            self.cookie_pool.delete_cookie(cookie)
            return
        for product_dic in data:
            _id = product_dic.get('id')
            otd = product_dic.get('otd')
            _time = product_dic.get('time')
            propId = product_dic.get('propID')
            img = product_dic.get('img') or ''
            dataID = product_dic.get('dataID')
            sellPrice = product_dic.get('sellPrice')
            hp = product_dic.get('hp')
            level = product_dic.get('level')
            quality = product_dic.get('quality')
            mint = product_dic.get('mint')
            addRatio = product_dic.get('addRatio')
            v1 = product_dic.get('v1')
            v2 = product_dic.get('v2')
            if 'shoeBox' in img:
                mk_text = '''新货上架：{}\t{}\t{}\t鞋盒'''.format(otd, sellPrice, quality)
            else:
                mk_text = '''新货上架：{}\t{}\t{}\t鞋子'''.format(otd, sellPrice, quality
                                                           )
            if sellPrice and otd:
                _sellPrice = sellPrice / 100000
                if otd<10000 or _sellPrice < 6:
                    self.robot.main(mk_text=mk_text)
                    time.sleep(5)

    def get_newest_gems_order_list(self):
        """
        获取最新数据
        """
        headers = deepcopy(self.headers)
        cookie = self.cookie_pool.get_cookie_str()
        print(f'cookie：{cookie}')
        headers.update({'cookie': cookie})
        newest_order_list_url = 'https://api.stepn.io/run/orderlist?order=2001&type=501&refresh=true'
        res = requests.get(newest_order_list_url, headers=headers, verify=False, proxies=self.proxies)

        data = res.json().get("data") or []
        if not data:
            self.cookie_pool.delete_cookie(cookie)
            return
        for product_dic in data:
            _id = product_dic.get('id')
            otd = product_dic.get('otd')
            _time = product_dic.get('time')
            propId = product_dic.get('propID')
            img = product_dic.get('img')
            dataID = product_dic.get('dataID')
            sellPrice = product_dic.get('sellPrice')
            hp = product_dic.get('hp')
            level = product_dic.get('level')
            quality = product_dic.get('quality')
            mint = product_dic.get('mint')
            addRatio = product_dic.get('addRatio')
            v1 = product_dic.get('v1')
            v2 = product_dic.get('v2')
            mk_text = '''新货上架：gems\t{}\t{}\t{}'''.format(otd, sellPrice, quality
                                       )
            if sellPrice:
                _sellPrice = sellPrice / 100000
                if _sellPrice > 9.8:
                    self.robot.main(mk_text=mk_text)
                    time.sleep(5)

    def get_newest_badges_order_list(self):
        """
        获取最新数据
        """
        headers = deepcopy(self.headers)
        cookie = self.cookie_pool.get_cookie_str()
        print(f'cookie：{cookie}')
        headers.update({'cookie': cookie})
        newest_order_list_url = 'https://api.stepn.io/run/orderlist?order=2001&type=501&refresh=true'
        res = requests.get(newest_order_list_url, headers=headers, verify=False, proxies=self.proxies)

        data = res.json().get("data") or []
        if not data:
            self.cookie_pool.delete_cookie(cookie)
            return
        for product_dic in data:
            _id = product_dic.get('id')
            otd = product_dic.get('otd')
            _time = product_dic.get('time')
            propId = product_dic.get('propID')
            img = product_dic.get('img')
            dataID = product_dic.get('dataID')
            sellPrice = product_dic.get('sellPrice')
            hp = product_dic.get('hp')
            level = product_dic.get('level')
            quality = product_dic.get('quality')
            mint = product_dic.get('mint')
            addRatio = product_dic.get('addRatio')
            v1 = product_dic.get('v1')
            v2 = product_dic.get('v2')
            mk_text = '''新货上架：gems\t{}\t{}\t{}'''.format(otd, sellPrice, quality
                                                         )
            if sellPrice:
                _sellPrice = sellPrice / 100000
                if _sellPrice > 9.8:
                    self.robot.main(mk_text=mk_text)
                    time.sleep(5)

    def main(self):
        while True:
            self.get_newest_sneakers_order_list()
            time.sleep(15)


if __name__ == '__main__':
    auto = AutoMsg()
    auto.main()
